#!/bin/sh

DIRECTORYPATH="${1}"

check_binary() {
	directoryfamily="${1}"
	filename="${2}"
	clamscan "${filename}" | grep "${family}" | cut -f 2 -d ":" | sed "s/^ //g" | cut -f 1 -d "-" | while read filefamily
	do
		if [ "${filefamily}" != "${directoryfamily}" ]
		then
			printf "%s in wrong directory: file: %s directory: %s\n" "${filename}" "${filefamily}" "${directoryfamily}"
		fi
	done
}

family="$(basename "${DIRECTORYPATH}")"
if [ -L "${DIRECTORYPATH}" ]
then
	printf "%s is a link\n" "${DIRECTORYPATH}"
else
	if [ -d "${DIRECTORYPATH}" ]
	then
		find "${DIRECTORYPATH}" -type f | while read filename
		do
			check_binary "${family}" "${filename}"
		done
		find "${DIRECTORYPATH}" -type l | while read filename
		do
			filename="$(readlink -f "${filename}")"
			check_binary "${family}" "${filename}"
		done
	fi
fi
